package com.pachong.android.frameworkbase.dialog;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.pachong.android.frameworkbase.R;

/**
 * Alert Dialog Fragment
 *
 * @Author: PengWeiwei
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2015-05-04
 * @Version: 1.0.0
 */
public class EditDialogFragment extends AlertDialogFragment {


    private CharSequence mDefaultContent;
    @StringRes
    private int mDefaultContentStrId;
    private EditText mEtContent;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        dialogLytCenterContainer.removeAllViews();

        View etContainer = inflater.inflate(R.layout.fwb_dialog_edittext, dialogLytCenterContainer, true);
        mEtContent = (EditText) etContainer.findViewById(R.id.dialogEtContent);

        setPositiveButton("确定", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.OnContentSaved(mEtContent.getText());
                }
                dismissAllowingStateLoss();
            }
        });

        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (!TextUtils.isEmpty(getDefaultContent())) {
            mEtContent.setText(getDefaultContent());
        }

        mBtnPositive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.OnContentSaved(mEtContent.getText());
                }

                if (mPositiveClickedListener != null) {
                    mPositiveClickedListener.onClick(v);
                }
            }
        });

    }

    public EditDialogFragment setDefaultContent(CharSequence charSequence) {
        mDefaultContent = charSequence;
        return this;
    }

    public EditDialogFragment setDefaultContent(@StringRes int contentStringId) {
        mDefaultContentStrId = contentStringId;
        return this;
    }


    CharSequence getDefaultContent() {
        if (mDefaultContentStrId > 0) {
            return getString(mDefaultContentStrId);
        } else {
            return mDefaultContent;
        }
    }


    private OnContentSavedListener mListener;
    public EditDialogFragment setOnCententSavedListener(OnContentSavedListener listener) {
        mListener = listener;
        return this;
    }

    public interface OnContentSavedListener {
        void OnContentSaved(CharSequence content);
    }


}
